<?php
/**
 * Copyright 2008 francissem (http://www.webmoss.net)(francissem@gmail.com)
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author francissem
 * @copyright 2008 francissem (http://www.webmoss.net)(francissem@gmail.com)
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */
$b1 = microtime(true);

define("__ROOT__",preg_replace("/\\\+/","/",dirname(__FILE__)));
$setting = parse_ini_file(__ROOT__."/conf.ini",true);

function pathjoin(){
	$args=func_get_args();
	$is_http=ereg("^http://",$args[0]);
	$prehtml="";
	if($is_http)$args[0]=ereg_replace("^http://","/",$args[0]);
	if($is_http) $prehtml="http:/";
	return $prehtml.ereg_replace("(/+)","/",str_replace("\\","/",implode("/",$args)));
}

define("__LIBRARY__",pathjoin(__ROOT__,$setting["structure"]["library_location"]));
define("__DATA__",pathjoin(__ROOT__,$setting["structure"]["data_location"]));
define("__MODULES__",pathjoin(__ROOT__,$setting["structure"]["modules_location"]));
define("__PUBLIC__",pathjoin(__ROOT__,$setting["structure"]["public_location"]));
define("__PUBLIC_URL__",preg_replace("/^".str_replace('/','\/',__ROOT__)."/","",__PUBLIC__));


function _throw_php_error($errno,$errstr,$errfile,$errline,$errcontext){
	throw new System_Exception($errstr,System_Exception::PHP_ERROR,$errfile,$errline);
}

set_include_path(get_include_path().PATH_SEPARATOR.__LIBRARY__);



function __autoload($cls){
	$phpfile = implode("/",explode("_",$cls)).".php";
	if(file_exists(__LIBRARY__."/".$phpfile)){
		require_once($phpfile);
		return;
	}
	require_once($cls.".php");
}

include_once(__LIBRARY__."/global.php");
require_once(__LIBRARY__."/Core/System.php");
require_once(__LIBRARY__."/Core/Module.php");
require_once(__LIBRARY__."/Core/Setting.php");


Core_System::addRepository("cache",Core_Repository::factory("FileSystem","Array",array("path"=>pathjoin(__DATA__,"cache/system"))));
Core_Module::addRepository("cache",Core_Repository::factory("FileSystem","Array",array("path"=>pathjoin(__DATA__,"cache/module"))));
Core_Setting::addRepository("override",Core_Repository::factory("FileSystem","Array",array("path"=>pathjoin(__DATA__,"setting/override"))));

Core_System::initSetting($setting);
$time_setting = Core_System::getSetting("time");
date_default_timezone_set($time_setting["timezone"]);

header("Content-Type: text/plain");
var_dump(Core_System::getSetting());

$setting = Core_System::getSetting();
$setting["test"]="tag_none";
$setting->save();

Core_Repository::commitAll();
exit;

if(preg_match("/^\/(_dev|_build)\/.*/",$_SERVER["REDIRECT_URL"])){
	Core_System::SetSetting("general","mode","debug");
}
if(Core_System::getSetting("general","mode")=="debug"){
	set_time_limit(0);
}



Core_System::DependPackage("Zend","1.6.0");
Core_System::DependPackage("Core","0.0.1");


Core_Module::load(__MODULES__);

if(preg_match("/^\/_build\/.*/",$_SERVER["REDIRECT_URL"])){
	$info=Core_Module::getAllModuleInfo();
	foreach(array_keys($info["module_infos"]) as $mname){
		$m = Core_Module::getModule($mname);
		$m->buildPublicResource();
	}
}

require_once("Core/Controller.php");
$request = Core_Controller::classifyRequest($_SERVER,array("_dev","_build","_preview"));
$controller = Core_Module::getModule($request->getModuleName())->getController();
$controller->setRequest($request);
$controller->response();

Core_Repository::commitAll();


//echo "[[ BE: ".(microtime(true)-$b1)." ]]";
?>
